<?php
/**
* @file $Id: User.fnc.php 458 2007-04-23 05:49:36Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

function User($item)
{	global $_FOCUS,$DefaultSyear;

	if(!$_SESSION['UserSyear'])
		$_SESSION['UserSyear'] = $DefaultSyear;

	if(!$_FOCUS['User'])
	{
		if($_SESSION['STUDENT_ID'])
		{
			$sql = "SELECT s.LAST_NAME||', '||s.FIRST_NAME AS NAME,'student' AS PROFILE,','||se.SCHOOL_ID||',' AS SCHOOLS,se.SYEAR,se.SCHOOL_ID,s.PROFILE_ID,s.USERNAME FROM STUDENTS s,STUDENT_ENROLLMENT se WHERE s.STUDENT_ID='$_SESSION[STUDENT_ID]' AND se.SYEAR='$_SESSION[UserSyear]' AND se.STUDENT_ID=s.STUDENT_ID ORDER BY se.END_DATE DESC LIMIT 1";
			$_FOCUS['User'] = DBGet(DBQuery($sql));
			$_SESSION['UserSchool'] = $_FOCUS['User'][1]['SCHOOL_ID'];
		}
		elseif($_SESSION['USERNAME'])
		{
			$sql = "SELECT STAFF_ID,USERNAME,FIRST_NAME||' '||LAST_NAME AS NAME,PROFILE,PROFILE_ID,SCHOOLS,CURRENT_SCHOOL_ID,SYEAR,LAST_NOTE_SEEN,to_char(LAST_LOGIN,'YYYY-MM-DD') AS LAST_LOGIN FROM USERS WHERE SYEAR='$_SESSION[UserSyear]' AND USERNAME='$_SESSION[USERNAME]'";
			$_FOCUS['User'] = DBGet(DBQuery($sql));
			// I'm not sure if this is quite correct.  This allows users to go backwards in time even if they didn't have access to the system in past years.  However, the user's staff ID will be the staff ID of the current year.
			if(!count($_FOCUS['User']))
			{
				$sql = "SELECT STAFF_ID,USERNAME,FIRST_NAME||' '||LAST_NAME AS NAME,PROFILE,PROFILE_ID,SCHOOLS,CURRENT_SCHOOL_ID,SYEAR,LAST_NOTE_SEEN,to_char(LAST_LOGIN,'YYYY-MM-DD') AS LAST_LOGIN FROM USERS WHERE SYEAR='$DefaultSyear' AND USERNAME='$_SESSION[USERNAME]'";
				$_FOCUS['User'] = DBGet(DBQuery($sql));
			}
		}
		//else
		//	exit('Error');
	}

	return $_FOCUS['User'][1][$item];
}
?>
